module scene {

    export class BeginAddPage extends eui.Component {
        constructor() {
            super();
            this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
            this.skinName = "BeginAddPageSkin";
        }

        private beginAddBtn: eui.Image

        private beginRejectBtn: eui.Label

        private onComplete() {

            this.beginRejectBtn.visible = false
            let timer = setTimeout(() => {
                clearTimeout(timer)
                this.beginRejectBtn.visible = true
            }, 500)

            this.beginAddBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onAdd, this)
            this.beginRejectBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onReject, this)
        }   

        private onAdd() {
            manager.StatisticsManager.click(148)
            let adManager = manager.GameManager.getInstance().getAdManager();
            let revivalMethod = Number(adManager.ad_percent) - Math.random() * 100
            if(revivalMethod >= 0){
                adManager.showVideoAd();
                adManager.addEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
            }else{
                manager.ShareManager.getInstance().addEventListener(manager.ShareManager.SHARE_STATUS,
                    this.onShareStatus, this);
                manager.GameManager.getInstance().commonShare();
            }
            console.log('onBeginAdd')
        }

        private onShareStatus(eve: egret.Event): void {
            manager.ShareManager.getInstance().removeEventListener(manager.ShareManager.SHARE_STATUS,
                this.onShareStatus, this);
            let succ: number = eve.data;
            if (succ > 0) {
                this.onShareSucc();
            }
        }

        private onVideoAdClose(eve: egret.Event): void {
            egret.log("视频广告播放事件:", eve.data);
            if (eve.data != null && eve.data.isEnded) {
                manager.GameManager.getInstance().adManager.removeEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
                this.onShareSucc();
            }
        }

        private onShareSucc(): void {
            let event: events.BeginAddEvent = new events.BeginAddEvent();
            this.dispatchEvent(event);
        }

        private onReject() { 
            manager.StatisticsManager.click(149)   
            console.log('onBeginReject')            
            let event: events.BeginAddRejectEvent = new events.BeginAddRejectEvent();
            this.dispatchEvent(event);
        }
    }
}